﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class OrdersController : ControllerBase
    {
        public OrdersController(){}
        public OrdersController(SkyfabDataContextDataContext sdc)
        {
            Sdc = sdc;
        }
        public void addOrder(int productId,string productNo, string productName,double netPrice, int quantity,string status) 
        {
            OrderEntity oe = new OrderEntity();
            oe.ProductId = productId;
            oe.NetPrice = (decimal)netPrice;
            oe.Quantity = quantity;
            oe.Amount = (decimal)netPrice * quantity;
            oe.Status = status;
            oe.Name = productName;
            oe.No = productNo;
            Order order = TransformOrderEntityToOrder(oe);
            Sdc.Orders.InsertOnSubmit(order);
            Sdc.SubmitChanges();
        }
        public static Order TransformOrderEntityToOrder(OrderEntity orderEntity)
        {
            Order order=new Order();
            order.ProductId=orderEntity.ProductId;
            order.NetPrice=orderEntity.NetPrice;
            order.Quantity=orderEntity.Quantity;
            order.Amount=orderEntity.Amount;
            order.Status = orderEntity.Status;
            order.Name = orderEntity.Name;
            order.No = orderEntity.No;
            return order;
        }
        public static OrderEntity TransformOrderToOrderEntity(Order order)
        {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.ProductId = order.ProductId;
            orderEntity.NetPrice = order.NetPrice;
            orderEntity.Quantity = order.Quantity;
            orderEntity.Amount = order.Amount;
            orderEntity.Status = order.Status;
            orderEntity.Name = order.Name;
            orderEntity.No = order.No;
            orderEntity.Id = order.Id;
            return orderEntity;
        }
        public IList<OrderEntity> findAllOrderEntities() {
            IList<Order> orders = (from o in Sdc.Orders
                                   select o).ToList();
            IList<OrderEntity> orderEntities = new List<OrderEntity>();
            foreach (var order in orders) {
                OrderEntity orderEntity = TransformOrderToOrderEntity(order);
                orderEntities.Add(orderEntity);
            }
            return orderEntities;

        }
        public IList<OrderEntity> findOrderEntitiesByStatus(string status)
        {

            IList<Order> orders = (from o in Sdc.Orders
                                   where o.Status == status
                                   select o).ToList();
            IList<OrderEntity> orderEntities = new List<OrderEntity>();
            foreach (var order in orders)
            {
                OrderEntity orderEntity = TransformOrderToOrderEntity(order);
                orderEntities.Add(orderEntity);
            }
            return orderEntities;

        }


        public void updateOrderStatusByOrderId(int orderId, string status)
        {
            Order order = Sdc.Orders.First(o => o.Id == orderId);
            order.Status = status;
            Sdc.SubmitChanges();
        }
    }
}
